package com.tang.Date20240611;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author tmh
 * @date 2024/6/11 22:18
 * @description
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
        //提交任务
        for (int i = 0; i < 20; i++) {
            final int taskId = i;
            executor.submit(()->{
                System.out.println("执行任务：" + taskId + "，线程名称：" + Thread.currentThread().getName());
                try {
                    Thread.sleep(2000); // 模拟任务执行时间
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        //关闭线程池
        executor.shutdown();

    }
}
